package com.tql.hualala.controller;

import com.mysql.cj.util.StringUtils;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.CommonEntity;
import com.tql.hualala.service.HualalaBillFoodService;
import com.tql.hualala.service.HualalaShopService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;


/**
 * 哗啦啦相关的接口
 */
@RestController
@RequestMapping("/hualala")
public class HualalaApiController extends BaseController {
    Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private HualalaShopService hualalaShopService;

    @Autowired
    private HualalaBillFoodService hualalaBillFoodService;


    // 1.拉取在哗啦啦平台开通的所有门店
    @PostMapping("/saveAllShop")
    public void saveAllShop()
    {
        hualalaShopService.saveAllShop();
    }
    //2.拉取门店的所有全量菜单信息
    @PostMapping("/saveShopMenu")
    public void saveShopMenu(@RequestParam("shopId") Long shopId)
    {
        hualalaShopService.saveShopMenu(shopId);
    }

    @PostMapping("/updateFood")
    public void updateFood(@RequestBody CommonEntity entity)
    {
        hualalaShopService.updateFood(entity);
    }
    //3.拉取某门店某天的历史账单
    @PostMapping("/saveShopBillDetail")
    public void saveShopBillDetail(@RequestBody CommonEntity entity) throws ParseException {
        hualalaBillFoodService.saveShopBillDetail(entity.getShopId(),entity.getReportDate());
    }
    //4.实时账单回调
    //该回调地址配置在哗啦啦云平台上,需要在gateway配置文件里过滤拦截
    @PostMapping(value = "/addBillData", consumes = "application/x-www-form-urlencoded;charset=UTF-8")
    public String addBillData(@RequestParam Map<String, String> params)
    {



           String data=params.get("data");
           if(StringUtils.isNullOrEmpty(data))
            return "{\"code\":\"111\"}";


           CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
               String traceId= "addBillData-"+UUID.fastUUID().toString();
               try {
                    List<String> shops=hualalaShopService.getEnabledShop();
                   System.out.println("异步任务开始执行...traceId:"+traceId);
                   hualalaBillFoodService.handleParam(data,shops,traceId);
                   System.out.println("异步任务执行完毕！...traceId:"+traceId);
               } catch (Exception e) {
                   e.printStackTrace();
                   String error=e.getMessage();
                   logger.info("1***********hualala/addBillData************{************{traceId}***{error}",traceId,error);
               }
           });



        return "{\"code\":\"000\"}";
    }



    // flag=1 表示 把指定月份所有数据执行一下
    //flag=2 执行每个特定的时间
    //flag=3 执行当前时间

    @PostMapping("/saveHllShopBillFoodReportday")
    public void saveHllShopBillFoodReportday(@RequestBody CommonEntity entity) throws ParseException {

        hualalaBillFoodService.saveShopBillReport(entity.getFlag(),entity.getReportDate());
    }

/**
 * insert into tql_hll_shop_bill_food_reportday3
 * (foodName,foodCode,reportDate,shopId,foodCount,isUpOutChange,foodRealPrice,foodRealAmount)
 * SELECT foodName,foodCode,reportDate,shopId,count(1),0,max(foodRealPrice),sum(foodRealAmount)
 *
 * from tql_hll_shop_bill_food3
 * group by foodCode,foodName,reportDate,shopId
 */
















}
